#install.packages('ggplot2')
library(ggplot2)
GOALS: Students should be able to use ggplot2 to generate publication quality graphics and understand and use the basics of the grammar of graphics.
library(ggplot2)
ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap)) +
geom_point()
NOTE:
ggplot function -any arguments we provide the ggplot function are considered global options: they apply to all layersggplot:aes function - which tells ggplot how variables map to aesthetic propertiesx & y locationsAlone the ggplot call isn’t enough to redner the plot.
ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap))
## If run, would produce an error.
Need to tell ggplot how we want to present variables by specifying a geom layer. In the above example we used geom_point to create a scatter plot.
ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap)) +
geom_point()
Using scatter plot not the best way to visualize change over time. Let’s use line plot.
ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country, color=continent)) +
geom_line()
added a by aesthetic to get a line per country and color by contintent
visualise both lines and points on the plot?
ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country, color=continent)) +
geom_line() + geom_point()
ggplot(data = gapminder, aes(x=year, y=lifeExp, by=country)) +
geom_line(aes(color=continent)) + geom_point()
ggplot to the geom_line layer so it no longer applies to the pointsggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap, color=continent)) +
geom_point()
ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap)) +
geom_point() + scale_y_log10()
log10 function applied a transformation to the values of the gdpPercap column before rendering them on the plotThis makes it easier to visualise the spread of data on the y-axis.
We can fit a simple relationship to the data by adding another layer, geom_smooth:
ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap)) +
geom_point() + scale_y_log10() + geom_smooth(method="lm")
geom_smooth layer:pwd <- ggplot(data = gapminder, aes(x = lifeExp, y = gdpPercap)) +
geom_point() + scale_y_log10() + geom_smooth(method="lm", size=1.5)
geom_smooth.aes function to define a mapping between data variables and their visual representation.ggplot(data = gapminder, aes(x = year, y = lifeExp, color=continent)) +
geom_line() + facet_wrap( ~ country)
facet_wrap layer took a “formula” as its argument, denoted by the tilde (~).ggplot(data = gapminder, aes(x = year, y = lifeExp, color=continent)) +
geom_line() + facet_wrap( ~ country) +
xlab("Year") + ylab("Life expectancy") + ggtitle("Figure 1") +
scale_fill_discrete(name="Continent") +
theme(axis.text.x=element_blank(), axis.ticks.x=element_blank())
This is just a taste of what you can do with ggplot2. RStudio provides a really useful cheat sheet of the different layers available, and more extensive documentation is available on the ggplot2 website. Finally, if you have no idea how to change something, a quick google search will usually send you to a relevant question and answer on Stack Overflow with reusable code to modify!